package com.fc.campusapplicationsystem.controller;

import com.fc.campusapplicationsystem.model.Superuser;
import com.fc.campusapplicationsystem.service.SuperuserService;
import com.fc.campusapplicationsystem.util.ResponseOBJ;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 贺哲
 * @2020-02-29 15:30
 */
@RestController
@RequestMapping("/superuserController")
public class SuperuserController {

    @Autowired
    SuperuserService superuserService;

    @GetMapping("/list")
    public ResponseOBJ<List<Superuser>> list(@RequestParam("page") Integer page,
                     @RequestParam("limit") Integer limit,
                     @RequestParam(value = "key", required = false) String key) {
        ResponseOBJ<List<Superuser>> responseOBJ = new ResponseOBJ<>();
        try {
            PageHelper.startPage(page, limit);
            List<Superuser> superusers = superuserService.list(key);
            PageInfo<Superuser> pageInfo = new PageInfo<>(superusers);
            responseOBJ.setData(superusers);
            responseOBJ.setCount(pageInfo.getTotal());
            responseOBJ.setMsg("查询成功");
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setMsg("查询失败");
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    /**
     * 新增管理员
     * @param superuser 管理员
     * @return  结果
     */
    @PostMapping("/add")
    public ResponseOBJ<Integer> add(@RequestBody Superuser superuser) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            if (superuserService.checkUsername(superuser)){
            Integer result = superuserService.add(superuser);
            responseOBJ.setData(result);
            responseOBJ.setMsg("添加成功!");
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
            }else {
                responseOBJ.setCode(ResponseOBJ.ERROR);
                responseOBJ.setMsg("添加失败,登录名已存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
            responseOBJ.setMsg("添加失败,请稍后再重试!");
        }
        return responseOBJ;
    }

    /**
     * 修改管理员
     * @param superuser 管理员
     * @return 结果
     */
    @PostMapping("/update")
    public ResponseOBJ<Integer> update(@RequestBody Superuser superuser) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            if (superuserService.checkUsername(superuser)){
            Integer result = superuserService.update(superuser);
            responseOBJ.setData(result);
            responseOBJ.setMsg("修改成功!");
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
            }else {
                responseOBJ.setCode(ResponseOBJ.ERROR);
                responseOBJ.setMsg("修改失败,登录名已存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
            responseOBJ.setMsg("修改失败,请稍后再重试!");
        }
        return responseOBJ;
    }

    @DeleteMapping("/delete")
    public ResponseOBJ<Integer> delete(@RequestParam("superuserId") Integer id) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            Integer result = superuserService.delete(id);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }
}
